package com.hedl.classroom.service.impl;

import com.alibaba.fastjson.JSON;
import com.hedl.classroom.base.exception.CommonResult;
import com.hedl.classroom.base.model.RestResponse;
import com.hedl.classroom.dto.XcCourseTablesDto;
import com.hedl.classroom.feignclient.ContentServiceClient;
import com.hedl.classroom.feignclient.MediaServiceClient;
import com.hedl.classroom.model.po.CoursePublish;
import com.hedl.classroom.model.po.Teachplan;
import com.hedl.classroom.service.LearningService;
import com.hedl.classroom.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Big Watermelon
 * @description
 * @date 2023/12/21/18:16
 */
@Slf4j
@Service
public class LearningServiceImpl implements LearningService {

    @Resource
    private MyCourseTablesService myCourseTablesService;

    @Resource
    private ContentServiceClient contentServiceClient;

    @Resource
    private MediaServiceClient mediaServiceClient;



    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
         if (coursepublish ==null){
            return RestResponse.validfail("课程不存在！");
        }
        //远程调用内容管理服务，根据课程计划id，（teachplanId）去查询课程计划信息，如果is_preview的值为1表示支持试学
        //也可以从coursepublish对象中解析出课程计划信息去判断是否支持试学
        //todo：如果支持试学，直接调用媒资服务查询视频播放地址，返回
        Teachplan teachplan = contentServiceClient.getTeachplan(teachplanId);
        if ("1".equals(teachplan.getIsPreview())){
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }
        //用户已登录
        if (StringUtils.isNotEmpty(userId)){
            //通过我的课程表
            XcCourseTablesDto xcCourseTablesDto = myCourseTablesService.getLearningStatus(userId, courseId);
            String learnStatus = xcCourseTablesDto.getLearnStatus();
            if ("702002".equals(learnStatus)){
                return RestResponse.validfail("无法学习，因为没有选课或选课后没有支付！");
            } else if ("702003".equals(learnStatus)) {
                return RestResponse.validfail("已过期需要申请续期或者重新支付！");
            }else {
                //有资格学习,要返回视频的播放地址
                //远程调用媒资获取视频播放地址
                RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return playUrlByMediaId;

            }

        }
        //用户没有登陆,取出课程的收费规则
        String charge = coursepublish.getCharge();
        if ("202000".equals(charge)){
            //有资格学习,要返回视频的播放地址
            //远程调用媒资获取视频播放地址
            RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return playUrlByMediaId;
        }

        return RestResponse.validfail("该课程需要购买！");
    }
}
